﻿/*
 * WangXin 2012年3月9日 12:17:39
 */
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Apps.Yunwei.YunWeiService;
using Microsoft.Expression.Interactivity.Core;
using QDP.Platform.Services;
using QDP.Platform.Silverlight;
namespace Apps.Yunwei.ViewModels
{
    public class MapBrowserViewModel : ObservableObject
    {
        private Dictionary<string, string> configs;
        private NorOpSvClient Client = null;
        private IList<ServiceObject> buffer = null;
        public MapBrowserViewModel()
        {
            this.Services = new ObservableCollection<ServiceObject>();
            this.Datas = new ObservableCollection<TopicItemObject>()
            {
                new TopicItemObject()
                {
                    ImageUrl1 = "/Resource/0.png",
                    Caption = "最近使用",
                },
                new TopicItemObject()
                {
                    ImageUrl1 = "/Resource/0.png",
                    Caption = "全部地图",
                    Tags= "all"
                },
                new TopicItemObject()
                {
                    ImageUrl1 = "/Resource/0.png",
                    Caption = "地图目录",
                    Tags="catalog"
                }
            };

            this.SelectedCommand = new ActionCommand(this.OnSelected);
        }
        public void SetConfig(Dictionary<string, string> cfgs)
        {
            this.configs = cfgs;
            this.Initialize();
        }

        private void Initialize()
        {
            Client = ServiceLoader.CreateWCFClient<NorOpSvClient>(configs["CC_URL"]);

            Client.GetServicesCompleted += Client_GetServicesCompleted;

            this.Refresh();
        }

        void Client_GetServicesCompleted(object sender, GetServicesCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.ShowMessage(e.Error.Message,e.Error.ToString());
            }
            else
            {
                FillTopicTree(buffer = e.Result);
            }
        }

        private TopicItemObject GetDataNode(string caption)
        {
            string[] tags = caption.Split('\\');
            TopicItemObject parentNode = Datas.First(p => p.Tags == "catalog");
            foreach (var tag in tags)
            {
                if (parentNode.Children.Any(p => p.Caption == tag))
                {
                    parentNode = parentNode.Children.First(p => p.Caption == tag);
                }
                else
                {
                    var temp = new TopicItemObject()
                    {
                        Caption = tag,
                        ImageUrl1 = "/Resource/0.png",
                        Parent = parentNode
                    };

                    if (parentNode == null)
                    {
                        temp.Tags = tag;
                        Datas.Add(temp);
                    }
                    else
                    {
                        temp.Tags = parentNode.Tags + "\\" + tag;
                        parentNode.Children.Add(temp);
                    }

                    parentNode = temp;
                }
            }
            return parentNode;
        }

        private void FillTopicTree(IList<ServiceObject> items)
        {
            foreach (var item in items)
            {
                GetDataNode(item.Tags);
            }
        }

        public ICommand SelectedCommand { get; private set; }
        // 角色清单
        public ObservableCollection<ServiceObject> Services { get; set; }
        // 用户清单
        public ObservableCollection<TopicItemObject> Datas { get; set; }

        private void Refresh()
        {
            Client.GetServicesAsync();
        }

        private void FillTopicData(string catalog)
        {
            catalog = catalog.Replace("catalog\\",string.Empty);
            Services.Clear();
            foreach (var item in buffer)
            {
                if (catalog == "all" || catalog == item.Tags)
                    Services.Add(item);
            }
        }
        // 节点选中指令处理函数
        private void OnSelected(object obj)
        {
            var item = obj as TopicItemObject;
            if (string.IsNullOrEmpty(item.Tags))
            { }
            else
            {
                FillTopicData(item.Tags);
            }
        }
    }
}